﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Collections;
using System.Data;

namespace Catering.Catering_Page.system
{
    public partial class Module_Manage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadRep();
            }
        }

        //加载Rep控件
        public void LoadRep()
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.Module().GetCountBywhere("1=1");
            DataTable dt_Personnel = new Catering_Rules.Module().GetList(this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex - 1, "1=1").Tables[0];
            this.Rep_Personnel.DataSource = dt_Personnel;
            this.Rep_Personnel.DataBind();
            Bind_Area_ddl(dr_ParentID, 0, 0, true);
        }

        private void Bind_Area_ddl(DropDownList ddl, int selectindex, int outid, bool isall)
        {
            ddl.Items.Clear();
            DataTable dt = new Catering_Rules.Module().GetChildNodeListByParentID(0);
            if (isall)
            {
                ListItem li = new ListItem("无", "0");
                ddl.Items.Add(li);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow rowinfo in dt.Rows)
                {
                    string moduleName = rowinfo["ModuleName"].ToString();//块名称
                    int moduleID = Convert.ToInt32(rowinfo["ID"]);
                    if (outid == moduleID) continue;
                    ListItem temp = new ListItem(moduleName, moduleID.ToString());
                    ddl.Items.Add(temp);
                }
                if (ddl.Items.Count > selectindex)
                {
                    ddl.SelectedIndex = selectindex;
                }
            }
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.LoadRep();
        }

        //添加模块
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Catering_Model.Module md = new Catering_Model.Module();
            md.ModuleName = this.txt_name.Text.Trim();
            md.Back = this.txt_back.Text.Trim();
            md.PageUrl = this.txt_Url.Text.Trim();
            md.id = new Catering_Rules.Module().GetMaxId();
            md.temp1 = Convert.ToInt32(this.dr_ParentID.SelectedValue);
            new Catering_Rules.Module().Add(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！')</script>");
            this.LoadRep();
        }

        //修改模块
        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            Catering_Model.Module md = new Catering_Model.Module();
            md.ModuleName = this.txt_name.Text.Trim();
            md.Back = this.txt_back.Text.Trim();
            md.PageUrl = this.txt_Url.Text.Trim();
            md.id = Convert.ToInt32(this.HidRid.Value);
            md.temp1 = Convert.ToInt32(this.dr_ParentID.SelectedValue);
            new Catering_Rules.Module().Update(md);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！')</script>");
            this.LoadRep();
        }

        //删除模块
        [WebMethod]
        public static string DelModule(string uid)
        {
            new Catering_Rules.Module().Delete(Convert.ToInt32(uid));
            return "OK";
        }

        //获取模块信息
        [WebMethod]
        public static ArrayList GetModule(string uid)
        {
            ArrayList al = new ArrayList();
            Catering_Model.Module md = new Catering_Rules.Module().GetModel(Convert.ToInt32(uid));
            al.Add(md.ModuleName);
            al.Add(md.Back);
            al.Add(md.PageUrl);
            al.Add(md.temp1);
            return al;
        }
        public string GetModuleName(string strid)
        {
            int id = 0;
            Int32.TryParse(strid, out id);
            if (id > 0)
            {
                Catering_Model.Module md = new Catering_Rules.Module().GetModel(Convert.ToInt32(id));
                return md.ModuleName;
            }
            else
            {
                return "无";
            }
        }
    }
}